﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace sqxt
{
    class fenye
    {
        public int pageSize = 50;     //每页显示行数                   
        public int nMax = 0;         //总记录数                       
        public int pageCount = 0;    //页数＝总记录数/每页显示行数    
        public int pageCurrent = 0;   //当前页号                      
        public int nCurrent = 0;      //当前记录行 
        DataGridView dgvInfo;
        BindingNavigator bdnInfo;
        BindingSource bdsInfo;
        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();

        public  void feny(DataGridView dv,BindingNavigator  bn, BindingSource bs,  string s)
        
        {
            dgvInfo = dv;
            bdnInfo = bn;
            bdsInfo = bs;
            dtInfo.Rows.Clear();
            dgvInfo.DataSource = dtInfo;
                 
                 SqlConnection conn = new SqlConnection(Dbconnect.s1);                          
                 conn.Open();                                                                                         
                 SqlDataAdapter sda = new SqlDataAdapter(s,conn);                     
                 sda.Fill(ds,"ds");                                                        
                conn.Close();                                                             
                dtInfo = ds.Tables[0];                                                    
                InitDataSet(); 
        }

        
                                                        
                                                                              
//3、用当前页面数据填充DataGridView                                             
                                                                              
    private void InitDataSet()                                                
    {                                                                         
        pageSize = 50;      //设置页面行数                                    
        nMax = dtInfo.Rows.Count;                                             
        pageCount=(nMax/pageSize);    //计算出总页数                          
        if ((nMax % pageSize) > 0) pageCount++;                               
        pageCurrent = 1;    //当前页数从1开始                                 
        nCurrent = 0;       //当前记录数从0开始                               
        LoadData();                                                           
     }                                                                        
                                                                              
    private void LoadData()                                                   
    {                                                                         
        int nStartPos = 0;   //当前页面开始记录行                             
        int nEndPos = 0;     //当前页面结束记录行                             
        DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架          
                                                                              
        if (pageCurrent == pageCount)                                         
        {                                                                     
            nEndPos = nMax;                                                   
        }                                                                     
        else                                                                  
        {                                                                     
             nEndPos = pageSize * pageCurrent;                                
        }                                                                     
                                                                              
        nStartPos = nCurrent;                                                                   
                                                                              
                                                                              
        //从元数据源复制记录行   
        if (nMax != 0)
        {
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
        }                                  
        bdsInfo.DataSource = dtTemp;                                          
        bdnInfo.BindingSource = bdsInfo;                                      
        dgvInfo.DataSource = bdsInfo;                                         
   }

        public void fanye(string sx)
        {
            if (sx =="上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页，请点击“下一页”查看！");
                    return ;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
            if (sx == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页，请点击“上一页”查看！");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();

            }
        }
        
    }
}
